package com.rxnetwork.sample.mvp.toolbar;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.rxnetwork.sample.R;
import com.rxnetwork.sample.utils.StatusBarUtil;

import me.imid.swipebacklayout.lib.SwipeBackLayout;

/**
 * by xiaocode
 * 带标题栏的activity类
 */
public abstract class BaseTopBarActivity extends BaseSwipeBackActivity {

    private TopbarLayout mTopBarLayout;

    private FrameLayout mContentLayout;

    // 右滑返回
    private SwipeBackLayout mSwipeBackLayout;

    private View getContentViewLayout() {
        View view = View.inflate(this, R.layout.activity_top_bar, null);
        mTopBarLayout = (TopbarLayout) view.findViewById(R.id.top_bar_layout);
        mTopBarLayout.setTitle(getTopTitle());
        try {
            //处理android 4.4以下会崩溃
            StatusBarUtil.setColor(this, mTopBarLayout.getTopbarBgColor() > 0 ? mTopBarLayout.getTopbarBgColor() :
                    getResources().getColor(android.R.color.white), 0);
        } catch (Exception e) {
        }
        mContentLayout = (FrameLayout) view.findViewById(R.id.content_layout);
        if (getContentViewResId() > 0) {
            View.inflate(this, getContentViewResId(), mContentLayout);
        }
        return view;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getContentViewResId() != 0) {
            setContentView(getContentViewLayout());
        }
        try {
            setTopBarLayoutColor(getResources().getColor(android.R.color.white));
        } catch (Exception e) {
        }
        initSwipeBack();
        initPresenter();
        initViewsAndEvents();
    }

    protected void initSwipeBack() {
        mSwipeBackLayout = getSwipeBackLayout();
        mSwipeBackLayout.setEdgeTrackingEnabled(SwipeBackLayout.EDGE_LEFT);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            mSwipeBackLayout.addSwipeListener(new SwipeBackLayout.SwipeListener() {
                @Override
                public void onScrollStateChange(int state, float scrollPercent) {
                    if (state == 0) {
                        //设置颜色，便于状态栏文字颜色 设置回来
                        setTopBarLayoutColor(mTopBarLayout.getTopbarBgColor());
                    }
                }

                @Override
                public void onEdgeTouch(int edgeFlag) {
                    StatusBarUtil.setTranslucent(BaseTopBarActivity.this);
                    StatusBarUtil.setStateBarColor(BaseTopBarActivity.this, mTopBarLayout.getTopbarBgColor());
                    //设置一遍margin 界面才会刷新，不解？
                    ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) mTopBarLayout.getLayoutParams();
                    mTopBarLayout.setLayoutParams(params);
                }

                @Override
                public void onScrollOverThreshold() {
                }
            });
        }
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        overridePendingTransition(R.anim.in_right, R.anim.out_left);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.in_left, R.anim.out_right);
    }

    public void setTopBarLayoutColor(int color) {
        mTopBarLayout.setTopbarBgColor(color);
        StatusBarUtil.setColor(this, color, 0);
        //设置一遍margin 界面才会刷新，不解？
        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) mTopBarLayout.getLayoutParams();
        mTopBarLayout.setLayoutParams(params);
    }

    public TopbarLayout getTopBarLayout() {
        return mTopBarLayout;
    }

    /**
     * 便于设置全局崩溃，网络错误，空白页
     *
     * @param contentLayout
     */
    public void setContentLayout(FrameLayout contentLayout) {
        this.mContentLayout = contentLayout;
    }

    public FrameLayout getContentLayout() {
        return mContentLayout;
    }

    protected abstract void initPresenter();

    protected abstract void initViewsAndEvents();

    protected abstract int getContentViewResId();

    protected abstract CharSequence getTopTitle();

}
